{% extends "squad/base.jinja2" %}

{% block page_title %}{{ _('Test history') }} {{ project.group.display_name }} / {{ project.display_name }} / {{ history.test }}{% endblock  %}

{% block content %}

  <h2 class="page-header well">
    <a class="h2 text-primitive" href="{{group_url(project.group)}}">{{project.group.display_name}}</a>
    » <a class="h2 text-primitive" href="{{project_url(project)}}">{{project.display_name}}</a>
    »
    {{history.test}}
    {% if request.GET.top %}
      up to {{history.top.version}}
    {% else %}
      {% if history.top %}
        <small>(<a href="?top={{history.top.version}}">{{ _('permalink') }}</a>)</small>
      {% endif %}
    {% endif %}
  </h2>
  {% if build is defined and status is defined %}
      {% include "squad/tests-details-nav.jinja2" %}
  {% endif %}
  {% with items=history %}
    {% include "squad/_pagination.jinja2" %}
  {% endwith %}

<h2> {{ _('Test results history') }} </h2>

  <table class='test-results'>

    <thead>
      <th>{{ _('Build') }}</th>
      <th>{{ _('Date') }}</th>
      {% for environment in history.environments %}
      <th>{{environment}}</th>
      {% endfor %}
    </thead>

    {% for build, results in history.results.items() %}
    <tr>
      <td><a href="{{project_url(build)}}">{{build.version}}</a></td>
      <td>{{build.datetime|date}}</td>
      {% for environment in history.environments %}
        {% with result=results[environment.id] %}
          {% if result %}
            {% with known_issues=result.known_issues %}
              <td class='{{result.status|slugify}}'>
                <a href="{{testrun_suite_test_details_url(project.group, project, build, result.test_run_status, result.test)}}">{{result.status}}{% if result.confidence_score %} ({{result.confidence_score}}%){% endif %}</a>
                {% if result.info['test_description'] or result.info['suite_instructions'] or result.info['test_instructions'] or result.info['test_log'] %}
                  <a href='#' data-toggle="modal" data-target="#info_modal" data-info="{{ to_json(result.info) }}"><span data-toggle="tooltip" data-placement="right" title="{{ _('Show info') }}" class='fa fa-info-circle'></span></a>
                {% endif %}
                {% if known_issues %}
                  <button type='button' class='known-issue btn btn-xs btn-info pull-right' data-toggle='popover'>
                    <i title='{{ _('Known issue') }}' class='fa fa-info-circle'></i>
                  </button>
                  <div class='known-issue-details' style='display: none'>
                    {% for known_issue in known_issues %}
                      {{ _('Known issue:') }}
                      {% if known_issue.url %}
                        <a href="{{known_issue.url}}">{{known_issue.title}}</a>
                      {% else %}
                        {{known_issue.title}}
                      {% endif %}
                      {% if known_issue.intermittent %}
                        {{ _('(intermittent)') }}
                      {% endif %}
                    {% endfor %}
                  </div>
                {% endif %}
              </td>
            {% endwith %}
          {% else %}
            <td>
              <i>{{ _('n/a') }}</i>
            </td>
          {% endif %}
        {% endwith %}
      {% endfor %}
    </tr>
    {% endfor %}

  </table>

  {% with items=history %}
  {% include "squad/_pagination.jinja2" %}
  {% endwith %}

<!-- Modal HTML -->
<div id="info_modal" class="modal fade" role="dialog">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title">{{ _('Test Info') }}</h4>
      </div>
      <div class="modal-body">
        <div id="test_description" style="display: none;">
          <p><strong>{{ _('Test description:') }}</strong></p>
          <div id="test_description_inner">
          </div>
        </div>
        <div id="test_reproduce" style="display: none;">
          <p><strong>{{ _('How to reproduce:') }}</strong></p>
          <div id="test_reproduce_inner">
          </div>
        </div>
        <div id="test_log" style="display: none;">
          <p><strong>{{ _('Test log:') }}</strong></p>
          <div id="test_log_inner">
            <pre></pre>
          </div>
        </div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal" ng-click="modalClosed()">{{ _('Close') }}</button>
      </div>
    </div>
  </div>
</div>

{% endblock %}
{% block javascript %}
<script type="text/javascript" src='{{static("squad/table.js")}}'></script>
<script type="text/javascript">
  jQuery(function($) {
    $('.known-issue').popover({
      placement: 'left',
      html: true,
      content: function() {
        return $(this).parent().find('.known-issue-details').html()
      }
    })
    $('[data-toggle="tooltip"]').tooltip();
    $('#info_modal').on('show.bs.modal', function (event) {
      var button = $(event.relatedTarget);
      var info = button.data('info');
      if(info['test_description']) {
        $('#test_description').show();
        $('#test_description_inner').html(info['test_description']);
      }
      if(info['suite_instructions'] || info['test_instructions']) {
        $('#test_reproduce').show();
        if(info['suite_instructions']) {
          $('#test_reproduce_inner').html(info['suite_instructions']);
        } else if (info['test_instructions']) {
          $('#test_reproduce_inner').html(info['test_instructions']);
        }
      }
      if(info['test_log']) {
        $('#test_log').show();
        $('#test_log_inner pre:first').html(info['test_log']);
      }
    })
  })
</script>
{% endblock %}
